package com.alipay.sofa.pay.dal.dao.mybatis;

import com.alipay.sofa.pay.dal.dao.PayOrderDAO;
import com.alipay.sofa.pay.dal.domain.PayOrderDO;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisPayOrderDAO extends SqlSessionDaoSupport implements PayOrderDAO {

    @Override
    public String createPayOrder(PayOrderDO payOrderDO) {
        getSqlSession().insert("insertPayOrder", payOrderDO);

        // TODO
        return null;
    }

    @Override
    public List<PayOrderDO> listPayOrders() {
        return getSqlSession().selectList("selectPayOrders");
    }

    @Override
    public List<PayOrderDO> listPayOrdersByUid(String userId) {
        Map<String, String> params = new HashMap<>();
        params.put("buyerId", userId);
        return getSqlSession().selectList("selectPayOrdersByUid", params);
    }

    @Override
    public PayOrderDO queryPayOrder(String orderNo) {
        Map<String, String> params = new HashMap<>();
        params.put("orderNo", orderNo);
        return getSqlSession().selectOne("selectPayOrder", params);
    }

    @Override
    public PayOrderDO queryPayOrderForUpdate(String orderNo) {
        Map<String, String> params = new HashMap<>();
        params.put("orderNo", orderNo);
        return getSqlSession().selectOne("selectPayOrderForUpdate", params);
    }

    @Override
    public boolean pay(String orderNo) {
        Map<String, String> params = new HashMap<>();
        params.put("orderNo", orderNo);
        return getSqlSession().update("finishPayOrder", params) == 1;
    }
}
